package com.wl.代码;



import com.entity.Studios;
import com.entity.Ticket;
import com.entity.User;
import com.jch.Dao.StudiosDao;
import com.jch.Dao.TicketDao;
import com.jch.Dao.UserDao;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class Movie extends JFrame {

    private JPanel jp_north;//北部面板
    private JLabel jl_cinema;//影院文本
    private JTextField jtf_cinema;//输入影院文本域
    private JButton jb_search;//搜索
    private JButton jb_return;//返回主页
    private JButton jb_selection;//选座购票


    private JPanel jp_south;//南部面板
    private UserDao userDao=new UserDao();
    int j=0;
    public Movie(User user) {

        //北部面板
        jp_north = new JPanel();
        jp_north.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 25));//设置网格布局
        jp_north.setPreferredSize(new Dimension(0, 70));
        add(BorderLayout.NORTH,jp_north);

        jp_south = new JPanel();
        jp_south.setLayout(null);//取消布局管理器
        jp_south.setPreferredSize(new Dimension(0, 530));

        JLabel jl_brougod = new JLabel();
        jl_brougod.setOpaque(true);
        jl_brougod.setBackground(new Color(200, 221, 242));
        jl_brougod.setBounds(0,70,1000,530);
        jp_south.add(jl_brougod);

        //影院
        jl_cinema = new JLabel("影院：",JLabel.CENTER);
        jl_cinema.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        jp_north.add(jl_cinema);

        //影院搜索框
        jtf_cinema = new JTextField(8);
        jtf_cinema.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jp_north.add(jtf_cinema);

        jb_search = new JButton("搜索");
        jb_search.setFont(new Font("微软雅黑", Font.PLAIN, 20));

        jp_north.add(jb_search);

        jb_return = new JButton("返回主页");
        jb_return.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jb_return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();

            }
        });
        jp_north.add(jb_return);

        jb_selection = new JButton("选座购票");
        jb_selection.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        jb_selection.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                dispose();
                JDialog dialog = new JDialog();
                dialog.setTitle("电影⭕订座");
                dialog.setSize(1000, 618);

                List<JButton> reset = new ArrayList<>();
                List<JButton> purchase = new ArrayList<>();

                // 标签
                JLabel background_1 = new JLabel(new ImageIcon(Movie.class.getResource("选座背景.jpg")));
                background_1.setBounds(0, 0, 1000, 618);
                dialog.add(background_1);

                //座位面板
                JPanel jp_upper = new JPanel();
                jp_upper.setLayout(new FlowLayout(FlowLayout.LEFT,18,5));
                jp_upper.setOpaque(false);
                jp_upper.setBounds(0,0,1000,490);
                background_1.add(BorderLayout.NORTH,jp_upper);

                //框
                JTextField jt_field = new JTextField();
                jt_field.setHorizontalAlignment(SwingConstants.CENTER);
                jt_field.setBounds(280,510,140,30);
                background_1.add(jt_field);

                //选取座次
                for (int i = 1; i <= 50; i++) {
                    JButton jButton = new JButton(i + "");
                    reset.add(jButton);
                    purchase.add(jButton);
                    jButton.setPreferredSize(new Dimension(80, 90));
                    jButton.setOpaque(true);
                    jButton.setForeground(Color.BLACK);
                    jButton.setBackground(Color.WHITE);

                    jp_upper.add(jButton);
                    jButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            jButton.setBackground(Color.RED);
                            jButton.setForeground(Color.RED);
                            jButton.setEnabled(false);
                            jt_field.setText(jt_field.getText() + jButton.getText() + ",");
                            j++;
                        }
                    });
                    jButton.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseEntered(MouseEvent e) {
                            // TODO Auto-generated method stub
                            jButton.setForeground(Color.RED);
                        }

                        @Override
                        public void mouseExited(MouseEvent e) {
                            // TODO Auto-generated method stub
                            jButton.setForeground(Color.BLACK);
                        }
                    });

                    //重置
                    JButton jb_Reset = new JButton("重置");
                    jb_Reset.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                    jb_Reset.setOpaque(false);
                    jb_Reset.setForeground(Color.black);
                    jb_Reset.setBounds(180,511,80,30);
                    jb_Reset.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {

                            jt_field.setText(null);
                            for (JButton button : reset) {
                                button.setOpaque(true);
                                button.setBackground(Color.WHITE);
                                button.setForeground(Color.BLACK);
                                button.setEnabled(true);
                            }
                        }
                    });
                    background_1.add(jb_Reset);



                    //购买
                    JButton jb_purchase = new JButton("购买");
                    jb_purchase.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                    jb_purchase.setOpaque(false);
                    jb_purchase.setForeground(Color.black);
                    jb_purchase.setBounds(440,510,80,30);
                    jb_purchase.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {


                            Toolkit.getDefaultToolkit().beep();
                            JOptionPane.showMessageDialog(dialog,"你购买的位置:"+jt_field.getText()
                                    +"\n 请留意电影放映时间");

                            if (user.getBalance()>(j*50)){

                                int i1 = userDao.updateReduceBalance((j * 50), user.getuName());
                                JOptionPane.showMessageDialog(dialog,"购买成功,余额:"+(user.getBalance()-(j*50)));


                                TicketDao ticketDao=new TicketDao();
                                /*Ticket ticket=new Ticket();
                                ticket.setUid(user.getUid());
                                ticket.setSid(j);
                                ticket.setSeat(Integer.parseInt(jt_field.getText().trim()));*/
                                String[] seats = jt_field.getText().trim().split(",");

                                Object[][] params = new Object[seats.length][3];
                                for (int k = 0; k < params.length; k++) {
                                    params[k][0] = user.getUid();
                                    params[k][1] = j;
                                    params[k][2] = Integer.parseInt(seats[k]);
                                }
                                int[] is = ticketDao.insertTicketBatch(params);

                                return;
                            }if(user.getBalance()<(j*50)) {
                                JOptionPane.showMessageDialog(dialog,"余额不足,请充值");
                                return;
                            }
                            jt_field.setText("");
                            for (JButton button : purchase) {
                                button.setBackground(Color.WHITE);
                                button.setForeground(Color.BLACK);
                            }

                        }
                    });
                    background_1.add(jb_purchase);
                }

                //返回上一层
                JButton jb_story = new JButton("返回上一层");
                jb_story.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                jb_story.setOpaque(false);
                jb_story.setForeground(Color.BLACK);
                jb_story.setBounds(540,510,120,30);
                jb_story.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        new Movie(user);
                        dialog.dispose();
                    }
                });
                background_1.add(jb_story);

                //返回主页
                JButton jb_homepage = new JButton("返回主页");
                jb_homepage.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                jb_homepage.setOpaque(false);
                jb_homepage.setForeground(Color.BLACK);
                jb_homepage.setBounds(680,510,100,30);
                jb_homepage.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        dialog.dispose();
                       new Movie(user).dispose();

                    }
                });
                background_1.add(jb_homepage);


                dialog.setLocationRelativeTo(null);
                dialog.setResizable(false);
                dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);// 关闭模式
                dialog.setVisible(true);// 设置窗口可见
                }

        });
        jp_north.add(jb_selection);




//         表头（列名）
        Object[] columnNames = {"场次编号", "场厅名称", "影院名称", "影院地址", "电影名称","开始时间","价格","剩余座位数"};

        StudiosDao studiosDao=new StudiosDao();
        List<Studios> studios = studiosDao.queryStudios();
        Object[][] data=new Object[studios.size()][];
        for (int i = 0; i < studios.size(); i++) {
            Studios studios1=studios.get(i);
            Object[] studiosData={studios1.getSid(),studios1.getsHallName(),studios1.getsCinemaName(),studios1.getsCinemaAddress(),
                    studios1.getsMovieTitle(),studios1.getSstartTime(),studios1.getsPrice(),studios1.getSsurplus()};
            data[i]=studiosData;

        }

//        rowData, columnNames
        // 创建一个表格，指定 所有行数据 和 表头
        JTable jt_showData = new JTable();
        jt_showData.setModel(new DefaultTableModel(data,columnNames));
        JTableHeader header = jt_showData.getTableHeader();
        header.setFont(new Font("微软雅黑",Font.PLAIN,12));
        header.setForeground(Color.MAGENTA);
        //设置单元格的样式
        DefaultTableCellRenderer d = new DefaultTableCellRenderer();
        //表格内容居中
        d.setHorizontalAlignment(JLabel.CENTER);
        jt_showData.setDefaultRenderer(Object.class, d);
        jt_showData.setRowHeight(30);//设置表格高度
        //设置表格字体样式
        jt_showData.setFont(new Font("楷体",Font.PLAIN,16));

        jt_showData.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        JPanel Lower_part = new JPanel();
        Lower_part.setBorder(new LineBorder(new Color(248,248,255),2));
        Lower_part.setBounds(60,30,870,360);

        // 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
        Lower_part.add(jt_showData.getTableHeader(),BorderLayout.NORTH);

        // 把 表格内容 添加到容器中心
        Lower_part.add(jt_showData, BorderLayout.CENTER);

        jl_brougod.add(Lower_part);

        add(BorderLayout.SOUTH,jp_south);


        setTitle("想看电影");
        setResizable(false);
        setSize(1000, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(Movie.DISPOSE_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
//
//        new Movie();
    }
}

